import configLite from 'config-lite'
import Mongolass from 'mongolass'
const config = configLite(__dirname)
const mongolass = new Mongolass()
const ObjectId = Mongolass.Types.ObjectId

mongolass.connect(config.mongodb)

exports.User = mongolass.model('User', {
  name: { type: 'string', required: true },
  password: { type: 'string', required: true },
  avatar: { type: 'string', required: true },
  gender: { type: 'string', enum: ['m', 'f', 'x'], default: 'x' },
  bio: { type: 'string', required: true }
})
exports.User.index({ name: 1 }, { unique: true }).exec()// 根据用户名找到用户，用户名全局唯一

const moment = require('moment')
const objectIdToTimestamp = require('objectid-to-timestamp')

// 根据 id 生成创建时间 created_at
mongolass.plugin('addCreatedAt', {
  afterFind: function (results) {
    results.forEach(function (item) {
      item.created_at = moment(objectIdToTimestamp(item._id)).format('YYYY-MM-DD HH:mm:ss')
    })
    return results
  },
  afterFindOne: function (result) {
    if (result) {
      result.created_at = moment(objectIdToTimestamp(result._id)).format('YYYY-MM-DD HH:mm:ss')
    }
    return result
  }
})

exports.Follow = mongolass.model('Follow', { // Follow
  author: { type: ObjectId, required: true },
  followId: { type: ObjectId, required: true },
  selfId: { type: ObjectId, required: true }
})

exports.Follow.index({ followId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Favorite = mongolass.model('Favorite', { // Favorite
  author: { type: ObjectId, required: true },
  favoriteId: { type: ObjectId, required: true },
  selfId: { type: ObjectId, required: true }
})

exports.Favorite.index({ favoriteId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Post = mongolass.model('Post', {
  author: { type: ObjectId, required: true },
  title: { type: 'string', required: true },
  content: { type: 'string', required: true },
  pv: { type: 'number', default: 0 }
})
exports.Post.index({ author: 1, _id: -1 }).exec()// 按创建时间降序查看用户的文章列表

exports.Ac = mongolass.model('Ac', {
  author: { type: ObjectId, required: true },
  title: { type: 'string', required: true },
  pic: { type: 'string', required: true },
  cname: { type: 'string', required: false },
  tags: { type: 'string', required: false },
  celebrity: { type: 'string', required: false },
  original: { type: 'string', required: false },
  content: { type: 'string', required: true },
  pv: { type: 'number', default: 0 }
})
exports.Ac.index({ author: 1, _id: -1 }).exec()// 按创建时间降序查看用户的动漫列表

exports.Ep = mongolass.model('Ep', {
  author: { type: ObjectId, required: true },
  acId: { type: ObjectId, required: true },
  title: { type: 'string', required: true },
  subTitle: { type: 'string', required: false },
  pic: { type: 'string', required: false },
  url: { type: 'string', required: false },
  tags: { type: 'string', required: false },
  source: { type: 'string', required: false },
  sort: { type: 'number', required: false },
  content: { type: 'string', required: false },
  pv: { type: 'number', default: 0 }
})
exports.Ep.index({ author: 1, _id: -1 }).exec()// 按创建时间降序查看用户的动漫剧集列表

exports.Play = mongolass.model('Play', {
  author: { type: ObjectId, required: true },
  acId: { type: ObjectId, required: true },
  epId: { type: ObjectId, required: true },
  title: { type: 'string', required: true },
  subTitle: { type: 'string', required: false },
  url: { type: 'string', required: false },
  tags: { type: 'string', required: false },
  source: { type: 'string', required: false },
  sort: { type: 'number', required: false },
  pv: { type: 'number', default: 0 }
})
exports.Play.index({ author: 1, _id: -1 }).exec()// 按创建时间降序查看用户的播放列表

exports.Comment = mongolass.model('Comment', { // 短评
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true },
  score: { type: 'number', required: true }
})

exports.Comment.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有留言，按创建时间升序

exports.Discuss = mongolass.model('Discuss', { // 讨论
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true }
})

exports.Discuss.index({ postId: 1, _id: 1 }).exec() // 通过 id 获取该所有，按创建时间升序

exports.Review = mongolass.model('Review', { // 长评
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true },
  score: { type: 'number', required: true }
})

exports.Review.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Question = mongolass.model('Question', { // 问题
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true }
})

exports.Question.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Star = mongolass.model('Star', { // 明星(演员)
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true }
})

exports.Star.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Role = mongolass.model('Role', { // 角色
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true }
})

exports.Role.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Trailer = mongolass.model('Trailer', { // 预告片
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true } //  1 预告片  2 片段  3 花絮
})

exports.Trailer.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Photo = mongolass.model('Photo', { // 图片
  author: { type: ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: ObjectId, required: true },
  replyId: { type: 'string', required: true },
  typeId: { type: 'number', required: true } // 1 剧照  2 海报 3 壁纸
})

exports.Photo.index({ postId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Tag = mongolass.model('Tag', { // Tag
  author: { type: ObjectId, required: true },
  tagId: { type: ObjectId, required: true },
  tagType: { type: 'string', required: true },
  tagName: { type: 'string', required: true }
})

exports.Tag.index({ tagId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序

exports.Collection = mongolass.model('Collection', { // Collection
  author: { type: ObjectId, required: true },
  Id: { type: ObjectId, required: true },
  collectionId: { type: ObjectId, required: true },
  collectionType: { type: 'string', required: true },
  collectionName: { type: 'string', required: true }
})

exports.Collection.index({ collectionId: 1, _id: 1 }).exec()// 通过 id 获取该所有，按创建时间升序
